package com.kbbywyclient.ui.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.data.entity.ModifyOfRegistPassWord;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.PwdModifyModule;
import com.kbbywyclient.presenter.PwdModifyPresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.PwdModifyContract;
import com.kbbywyclient.util.CheckFormat;
import com.kbbywyclient.util.ToastUtil;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.CustomToolBar;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description:
 * Created by FHW
 * on 2017/5/8
 */

public class PwdModityActivity extends BaseActivity implements PwdModifyContract.View {

    @BindView(R.id.v_toolbar)
    CustomToolBar v_toolbar;

    @BindView(R.id.et_old_pwd)
    EditText et_old_pwd;
    @BindView(R.id.et_new_pwd)
    EditText et_new_pwd;
    @BindView(R.id.et_new_pwd_again)
    EditText et_new_pwd_again;
    @BindView(R.id.tv_determine)
    TextView tv_determine;

    private String str_pwd = "";
    private String str_pwd_again = "";

    private String str_old_pwd = "";

    @Inject
    PwdModifyPresenter pwdModifyPresenter;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_pwd_moditfy;
    }

    @OnClick({R.id.tv_determine})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_determine:
                if (vertify()) {
                    pwdModifyPresenter.getModifyOfRegistPassWord(PreUtils.getUser(this).getSalesID() + ""
                            , PreUtils.getUserAccount(this), str_pwd, "2", "", str_old_pwd);
                }
                break;
        }
    }

    /**
     * 验证密码
     *
     * @return
     */
    private boolean vertify() {
        if (str_old_pwd.isEmpty()) {
            ToastUtil.showShort("请输入旧密码密码");
            return false;
        }
        if (!CheckFormat.isPwd(str_old_pwd)) {
            ToastUtil.showShort("密码须为6-15位");
            return false;
        }
        if (str_pwd.isEmpty()) {
            ToastUtil.showShort("请输入新密码");
            return false;
        }
        if (!CheckFormat.isPwd(str_pwd)) {
            ToastUtil.showShort("密码须为6-15位");
            return false;
        }
        if (str_pwd_again.isEmpty()) {
            ToastUtil.showShort("请再次输入新密码");
            return false;
        }
        if (!str_pwd.equals(str_pwd_again)) {
            ToastUtil.showShort("密码和确认密码设置未统一");
            return false;
        }

        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("修改密码");
    }

    @Override
    protected void initView() {
        v_toolbar.setLeftViewOnClickLinster(v -> finish());

        et_old_pwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                str_old_pwd = s.toString();
                setBgView();
            }
        });
        et_new_pwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                str_pwd = s.toString();
                setBgView();
            }
        });
        et_new_pwd_again.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                str_pwd_again = s.toString();
                setBgView();
            }
        });

    }

    private void setBgView() {
        if (CheckFormat.isPwd(str_old_pwd) && CheckFormat.isPwd(str_pwd) && CheckFormat.isPwd(str_pwd_again)) {
            tv_determine.setBackgroundResource(R.drawable.textview_bg_14c095);
        } else {
            tv_determine.setBackgroundResource(R.drawable.textview_bg_dddddd);
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getPwdModifyComponent(new PwdModifyModule(this)).inject(this);
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void loadSucModifyOfRegist(ModifyOfRegistPassWord modifyOfRegistPassWord) {
        ToastUtil.showShort("修改成功");
        finish();
    }

    @Override
    public void loadFaiModifyOfRegist(String errorMsg) {

    }
}
